<?php


namespace app\forms\api;

use app\core\response\ApiCode;
use app\forms\common\CommonOption;
use app\forms\common\CommonQrCode;
use app\forms\common\grafika\ApiGrafika;
use app\forms\common\grafika\CommonFunction;
use app\forms\common\grafika\GrafikaOption;
use app\forms\mall\poster\PosterForm;
use app\models\Goods;
use app\models\Topic;

class ApiQrcodeForm extends GrafikaOption
{
    public $goods_id;
    public $id;
    public $num;
    public function rules()
    {
        return [
            [['goods_id', 'num'], 'integer'],
        ];
    }

    public function qrcode($method)
    {
        try {
            if (method_exists($this, $method)) {
                return [
                    'code' => ApiCode::CODE_SUCCESS,
                    'data' => $this->$method()
                ];
            }
        } catch (\Exception $e) {
            dd($e);
            return [
                'code' => ApiCode::CODE_ERROR,
                'msg' => $e->getMessage(),
                'line' => $e->getLine(),
            ];
        }
    }

    public function default()
    {
        return (new PosterForm())->getDefault();

    }
    public function goods()
    {
        $common = new GrafikaOption();
        $option = $common->optionDiff('goods');

        $editor = new ApiGrafika();

        $goods = Goods::find()->where([
            'is_delete' => 0,
            'id' => 48,
        ])->with(['goodsWarehouse', 'attr', 'mallGoods'])->one();
        if (!$goods) {
            throw new \Exception('商品不存在');
        }

        foreach($option as $key => $item) {
            if($key == 'qr_code') {
                $code = (new CommonQrCode())->getQrCode(['id' => 1], $option['qr_code']['size'], 'pages/index/index');
                $option['qr_code']['file_path'] = $code['file_path'];
            }
            if($key == 'pic') {
                $option['pic']['file_path'] = $goods->goodsWarehouse->cover_pic;
            }
            if($key == 'price') {
                $price = [];
                foreach ($goods->attr as $attr) {
                    $price[] = $attr['price'];
                }
                if ($goods->mallGoods['is_negotiable']) {
                    $price_str = '价格面议';
                } else {
                    $price_str = max($price) > min($price) ? '￥' . min($price) . '~' . max($price) : '￥' . min($price);
                }
                $option['price']['text'] = $price_str;
            }

            if($key == 'name') {
                $name = CommonFunction::autowrap($option['name']['font'], 0, $editor->font_path, $goods->goodsWarehouse->name, 670, 2);
                $option['name']['text'] = $name;
            }
        }
        $editor = $common->getPoster($option);
        return ['pic_url' => $editor->qrcode_url . '?v=' . time()];
    }

    public function share()
    {
        $common = new GrafikaOption();
        $option = $common->optionDiff('share');

        foreach($option as $key => $item) {
            if($key == 'qr_code') {
                $code = (new CommonQrCode())->getQrCode(['id' => 1], $option['qr_code']['size'], 'pages/index/index');
                $option['qr_code']['file_path'] = $code['file_path'];
            }
            if($key == 'name') {
                $option['name']['text'] = \Yii::$app->user->identity->nickname;
            }
        }
        $editor = $common->getPoster($option);
        return ['pic_url' => $editor->qrcode_url . '?v=' . time()];
    }

    public function other(&$goods_qrcode,ApiGrafika $editor, array $option)
    {
        $option_hb_down = \Yii::$app->basePath . '/web/statics/img/mall/topic-hb-down.png';
        $text = imagettfbbox($option['open_desc']['font'], 0, $editor->font_path, $option['open_desc']['text']);

        $editor->apiBlend($goods_qrcode, $goodsDownm, $option_hb_down, 24, 24, 'normal', 1, 'top-left', $text[2] - $text[0], $option['open_desc']['top'] + 40);
    }

    public function topic()
    {
        $option = $this->optionDiff('topic');

        $editor = new ApiGrafika();

        $topic = Topic::findOne($this->id);
        if (!$topic) {
            throw new \Exception('专题不存在');
        }

        foreach($option as $key => $item) {
            if($key == 'qr_code') {
                $code = (new CommonQrCode())->getQrCode(['id' => 1], $option['qr_code']['size'], 'pages/index/index');
                $option['qr_code']['file_path'] = $code['file_path'];
            }
            if($key == 'pic') {
                $option['pic']['file_path'] = $topic['qrcode_pic'] ?: $topic['cover_pic'];
            }
            if($key == 'content') {
                $content = strip_tags($topic['content']);
                $content = str_replace(array("\r\n", "\r", "\n", "&nbsp;", " "), "", $content);
                $content = CommonFunction::autowrap($option[$key]['font'], 0, $editor->font_path, $content, 670, 2);
                $option[$key]['text'] = $content;
            }

            if($key == 'title') {

                $title = CommonFunction::autowrap($option[$key]['font'], 0, $editor->font_path,$topic['title'], 670, 2);
                $option[$key]['text'] = $title;
            }
            if($key == 'look') {
                $read = $topic['virtual_read_count'] + $topic['read_count'];
                $read = $read > 10000 ? ($read / 10000) . '万+人浏览' : $read . '人浏览';
                $option[$key]['text'] = $read;
            }
        }

        $editor = $this->getPoster($option);
        return ['pic_url' => $editor->qrcode_url . '?v=' . time()];
    }

}